package Clases;

public class Filtrar {
	private float _offset;
	private float _ax;
	private float _ay; 
	private float _az;
	private float _ALFA;
	private float _limitAcep;
	
	public Filtrar(float alfa, float limite, float off){
		_ALFA = alfa;
		_limitAcep = limite;
		_offset = off;
		_ax = (float)0.0;
		_ay = (float)0.0;
		_az = (float)0.0;
	}
	
	public void setAlfa(float alfa){
		_ALFA = alfa;
	}
	
	public void setLimite(float limit){
		_limitAcep = limit;
	}
	
	public void setOffset(float off){
		_offset = off;
	}
	
	public float [] filtrar(float x, float y, float z){
		float [] ret = new float[3];
		//Calibrar
		if(x != 0)
			x = (x > 0) ? x - _offset : x + _offset;
		if(y != 0)
			y = (y > 0) ? y - _offset : y + _offset;
		if(z != 0)
			z = (z > 0) ? z - _offset : z + _offset;
		//Filtro pasa baja
		_ax = (float)(_ALFA * x + (_ALFA - 1) * _ax);
		_ay = (float)(_ALFA * y + (_ALFA - 1) * _ay);
		_az = (float)(_ALFA * z + (_ALFA - 1) * _az);
		//Aplanar ruido
		if(_ax < _limitAcep && _ax > -_limitAcep) _ax = (float)0.0;
		if(_ay < _limitAcep && _ay > -_limitAcep) _ay = (float)0.0;
		if(_az < _limitAcep && _az > -_limitAcep) _az = (float)0.0;
		ret[0] = _ax;
		ret[1] = _ay;
		ret[2] = _az;
		return ret;
	}
}
